﻿package map 
{
	
	import cmodule.PathFinder.CLibInit;
	/**
	 * ...
	 * @author fox
	 * 
	 */
	public class PathFinder 
	{
		private var lib:Object = null;
		private var maxX:int = 0;
		private var maxY:int = 0;
		private var array:Array = null;
		
		
		public function PathFinder() 
		{
			
		}
		
		
		public function init():void
		{
			var test:CLibInit = new CLibInit();
			lib = test.init();
			
		}
		
		public function release():void
		{
			lib = null;
		}
		
		public function initMap( data:String , maxX:int , maxY:int ):void
		{
			lib.init( data , maxX , maxY );
			
			this.maxX = maxX;
			this.maxY = maxY;
		}
		
		public function findPath( x1:int , y1:int , x2:int , y2:int ):Array
		{
			array = lib.findPath( x1 , y1 , x2 , y2 );
			
			var result:Array = new Array();
			
			for (var i:int = 0; i < array.length ; i++) 
			{
				result.push( new MapGrid( array[i] % maxX , array[i] / maxX ) );
			}
			
			return result;
		}
		
		
		public static var instance:PathFinder = null;
		public static function Instance():PathFinder
		{
			if ( instance == null )
			{
				instance = new PathFinder();
			}
			
			return instance;
		}
	}
	
}